<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\cdc;

use app\enum\CommonEnum;
use app\enum\ServiceCostEnum;
use app\model\mongo\MongoKsPicSyncLogModel;
use app\model\mongo\MongoKsVideoSyncLogModel;
use app\model\mongo\MongoOePicSyncLogModel;
use app\model\mongo\MongoOeVideoSyncLogModel;
use app\model\mongo\MongoQqPicSyncLogModel;
use app\model\mongo\MongoQqVideoSyncLogModel;
use app\service\BaseService;
use app\service\cdc\material\KsPicService;
use app\service\cdc\material\KsVideoService;
use app\service\cdc\material\OePicService;
use app\service\cdc\material\OeVideoService;
use app\service\cdc\material\QqPicService;
use app\service\cdc\material\QqVideoService;

final class MaterialService extends BaseService
{
    protected int $mediaId = 0;

    protected int $taskType = 0;

    private ?BaseService $service;

    public function __construct(int $mediaId = 0, int $taskType = 0)
    {
        $this->mediaId = $mediaId;
        $this->taskType = $taskType;
    }

    /**
     * 获取对象.
     * @return bool|KsPicService|KsVideoService|OePicService|OeVideoService|QqPicService|QqVideoService
     */
    public function getObject()
    {
        if ($this->mediaId == CommonEnum::OE_MEDIA) {
            if ($this->taskType == ServiceCostEnum::TASK_TYPE_VIDEO) {
                return $this->service = new OeVideoService();
            }
            if ($this->taskType == ServiceCostEnum::TASK_TYPE_PICTURE) {
                return $this->service = new OePicService();
            }
        } elseif ($this->mediaId == CommonEnum::KS_MEDIA) {
            if ($this->taskType == ServiceCostEnum::TASK_TYPE_VIDEO) {
                return $this->service = new KsVideoService();
            }
            if ($this->taskType == ServiceCostEnum::TASK_TYPE_PICTURE) {
                return $this->service = new KsPicService();
            }
        } elseif ($this->mediaId == CommonEnum::QQ_MEDIA) {
            if ($this->taskType == ServiceCostEnum::TASK_TYPE_VIDEO) {
                return $this->service = new QqVideoService();
            }
            if ($this->taskType == ServiceCostEnum::TASK_TYPE_PICTURE) {
                return $this->service = new QqPicService();
            }
        }

        return self::setError('未知媒体类型');
    }

    /**
     * 获取类型列表.
     */
    public function getTypeList(): array
    {
        if ($this->taskType == 2) {
            return $this->getVideoTypeList();
        }

        if ($this->taskType == 3) {
            return $this->getPicTypeList();
        }
    }

    /**
     * 获取列表.
     * @return mixed
     */
    public function getList(array $params = [])
    {
        return $this->service::getList($params);
    }

    /**
     * 更新同步状态
     * @param string $id 主键ID
     */
    public function updateSync(string $id = ''): bool
    {
        return $this->service::updateSync($id) <= 0;
    }

    /**
     * 图片类型列表.
     */
    protected function getPicTypeList(): array
    {
        if ($this->mediaId == CommonEnum::OE_MEDIA) {
            return [
                MongoOePicSyncLogModel::TYPE_ACCOUNT,
                MongoOePicSyncLogModel::TYPE_CLIENT,
                MongoOePicSyncLogModel::TYPE_PIC,
            ];
        }
        if ($this->mediaId == CommonEnum::KS_MEDIA) {
            return [
                MongoKsPicSyncLogModel::TYPE_ACCOUNT,
                MongoKsPicSyncLogModel::TYPE_CLIENT,
                MongoKsPicSyncLogModel::TYPE_PIC,
            ];
        }

        return [
            MongoQqPicSyncLogModel::TYPE_ACCOUNT,
            MongoQqPicSyncLogModel::TYPE_CLIENT,
            MongoQqPicSyncLogModel::TYPE_PIC,
        ];
    }

    /**
     * 视频类型列表.
     */
    protected function getVideoTypeList(): array
    {
        if ($this->mediaId == CommonEnum::OE_MEDIA) {
            return [
                MongoOeVideoSyncLogModel::TYPE_ACCOUNT,
                MongoOeVideoSyncLogModel::TYPE_CLIENT,
                MongoOeVideoSyncLogModel::TYPE_VIDEO,
            ];
        }
        if ($this->mediaId == CommonEnum::KS_MEDIA) {
            return [
                MongoKsVideoSyncLogModel::TYPE_ACCOUNT,
                MongoKsVideoSyncLogModel::TYPE_CLIENT,
                MongoKsVideoSyncLogModel::TYPE_KS_USER,
                MongoKsVideoSyncLogModel::TYPE_VIDEO,
            ];
        }

        return [
            MongoQqVideoSyncLogModel::TYPE_ACCOUNT,
            MongoQqVideoSyncLogModel::TYPE_CLIENT,
            MongoQqVideoSyncLogModel::TYPE_VIDEO,
        ];
    }


    /**
     * 删除数据
     * @param array $params 参数
     * @return mixed
     */
    public function deleteData(array $params = []){
        return $this->service::deleteData($params);
    }

    /**
     * 修改数据
     * @param array $params 参数
     * @param array $data 数据
     * @return mixed
     */
    public function updateData(array $params = [],array $data = []){
        return $this->service::updateData($params,$data);
    }

    /**
     * 获取信息
     * @param array $params 参数
     * @return mixed
     */
    public function getInfo(array $params = []){
        return $this->service::getInfo($params);
    }
}
